package com.definesys.smecrailtransit.module.TotalTime.PopWindowAdapter;

import android.animation.ValueAnimator;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutPopwindowItemBinding;
import com.definesys.smecrailtransit.module.TotalTime.bean.ElevatorBean;
import com.definesys.smecrailtransit.module.TotalTime.dialog.TotalTimeDialog;
import com.hwangjr.rxbus.Bus;

import java.util.ArrayList;
import java.util.WeakHashMap;

/**
 * Created by cjy on 2017/8/21.
 */

public class PopWindowAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private ArrayList<ElevatorBean> list;
    private int maxNumber;
    private DisplayMetrics dm;
    protected Bus mRxbus;
    public boolean showAnimation;

    public PopWindowAdapter(DisplayMetrics dm, Bus mRxbus){

        this.list = new ArrayList<>();
        this.dm = dm;
        this.mRxbus = mRxbus;
        this.showAnimation = true;
    }

    public void setList(ArrayList<ElevatorBean> list){
        if(list != null && list.size() != 0){
            this.list = list;
            this.maxNumber = this.list.get(0).getValue();
            for(int i=1 ;i < this.list.size(); i++){
                if(maxNumber < this.list.get(i).getValue()){
                    maxNumber = this.list.get(i).getValue();
                }
            }
        }else{
            this.list = new ArrayList<>();
        }
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutPopwindowItemBinding layoutPopweindowItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                , R.layout.layout_popwindow_item,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutPopweindowItemBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutPopweindowItemBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        LayoutPopwindowItemBinding viewDataBinding=(LayoutPopwindowItemBinding)holder.getBinding();
        viewDataBinding.text.setText(list.get(position).getEleCode());
        viewDataBinding.number.setText(String.valueOf(list.get(position).getValue())+"小时");

        if(maxNumber != 0){
            int width = this.dm.widthPixels*1/5*this.list.get(position).getValue()/this.maxNumber;
            if(showAnimation){
                //属性动画对象
                ValueAnimator va ;
                //显示view，高度从0变到height值
                va = ValueAnimator.ofInt(0,width);
                va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        //获取当前的height值
                        int w =(Integer)valueAnimator.getAnimatedValue();
                        //动态更新view的高度
                        viewDataBinding.pillar.getLayoutParams().width = w;
                        viewDataBinding.pillar.requestLayout();
                    }
                });
                va.setDuration(800);
                //开始动画
                va.start();
            }else{
                viewDataBinding.pillar.getLayoutParams().width = width;
            }

        }else{
            viewDataBinding.pillar.getLayoutParams().width = 0;
        }

//        CommonUtils.AddAnimation(viewDataBinding.pillar, 1.0f,
//                viewDataBinding.pillar.getScaleY(),fragmentMonthFormBinding.getRoot().getContext());

        viewDataBinding.item.setOnClickListener(v -> {
            WeakHashMap<String,String> map = new WeakHashMap<>();
            map.put("eleContractNo",list.get(position).getEleContractNo());
            map.put("eleCode",list.get(position).getEleCode());
            mRxbus.post(TotalTimeDialog.SHOW_ELEVATOR_TIME, map);
        } );
        viewDataBinding.invalidateAll();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
}

